import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_oa/utils/const.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';

class MyImagePicker extends StatefulWidget {
  final String type;

  MyImagePicker({String type}) : this.type = type;

  @override
  _MyImagePickerState createState() => _MyImagePickerState(type: type);
}

class _MyImagePickerState extends State<MyImagePicker> {
  String type;
  var _imgPath;

  _MyImagePickerState({String type}) : this.type = type;

  /*拍照*/
  _takePhoto() async {
    File image = await ImagePicker.pickImage(source: ImageSource.camera);
    setState(() {
      _imgPath = image;
    });
  }

/*相册*/
  _openGallery() async {
    File image = await ImagePicker.pickImage(source: ImageSource.gallery);
    setState(() {
      _imgPath = image;
    });
  }

  /*图片控件*/
  Widget _ImageView(imgPath) {
    if (imgPath == null) {
      return Center(
        child: Icon(
          Icons.image,
          color: Colors.grey,
          size: 200,
        ),
      );
    } else {
      return Image.file(
        imgPath,
      );
    }
  }

  @override
  void initState() {
    super.initState();
    if (type == Const.Camera) {
      _takePhoto();
    } else if (type == Const.Picture) {
      _openGallery();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(
          '裁剪图像',
          style: TextStyle(color: Colors.black),
        ),
        backgroundColor: Colors.grey[200],
        leading: IconButton(
            icon: Icon(
              Icons.arrow_back_ios,
              color: Colors.black,
            ),
            onPressed: () {
              Navigator.pop(context);
            }),
        elevation: 0.0,
      ),
      body: Container(
        padding: EdgeInsets.all(20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            _ImageView(_imgPath),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                GestureDetector(
                    onTap: () {
                      _cropImage();
                    },
                    child: Text(
                      "裁剪",
                      style: TextStyle(fontSize: 20),
                    )),
                GestureDetector(
                    onTap: () {
                      Navigator.pop(context, _imgPath);
                    },
                    child: Text(
                      "确定",
                      style: TextStyle(fontSize: 20),
                    )),
              ],
            )
          ],
        ),
      ),
    );
  }

  Future<Null> _cropImage() async {
    File croppedFile = await ImageCropper.cropImage(
      sourcePath: _imgPath.path,
      toolbarTitle: 'Cropper',
      toolbarColor: Colors.blue,
      toolbarWidgetColor: Colors.white,
    );
    if (croppedFile != null) {
      File image = croppedFile;
      setState(() {
        _imgPath = image;
      });
    }
  }

//  void _clearImage() {
//    imageFile = null;
//    setState(() {
//      state = AppState.free;
//    });
//  }
}
